package com.superteam.zzl.activity;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.superteam.zzl.activity.databinding.ActivityFirstWelcomeBinding;
import com.superteam.zzl.utils.ImageCarousel;

import java.io.InputStream;
import java.util.ArrayList;

public class FirstWelcomeActivity extends BaseActivity {

    private ConvenientBanner banner;
    private ActivityFirstWelcomeBinding binding;
    private ArrayList<Bitmap> bitmaps;
    private ImageCarousel imageCarousel;

    @Override
    public void widgetClick(View v) {
        startActivity(new Intent(this,MainActivity.class));
        finish();
    }

    @Override
    public ViewDataBinding initDataBinding() {
        binding = DataBindingUtil.setContentView(this,R.layout.activity_first_welcome);
        banner = binding.bannerWelcome;
        bitmaps = new ArrayList<>();
        loadImage();
        return binding;
    }

    @Override
    public void initParms(Bundle parms) {

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public void initView(View view) {

    }

    @Override
    public void setListener() {

    }

    @Override
    public void doBusiness(Context mContext) {

    }

    //图片轮播
    private void loadImage(){
        bitmaps.add(readBitMap(this,R.drawable.start_guide_1));
        bitmaps.add(readBitMap(this,R.drawable.start_guide_2));
        bitmaps.add(readBitMap(this,R.drawable.start_guide_3));
        bitmaps.add(readBitMap(this,R.drawable.start_guide_4));

        banner.setCanLoop(true);
        imageCarousel = new ImageCarousel(banner,bitmaps);
        banner.setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
        banner.setCanLoop(false);
        banner.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Log.e("data",position+"");
            }
        });
    }



    @Override
    protected void onResume() {
        super.onResume();
        banner.startTurning(2000);//设置开始轮播以及轮播时间
    }


    /**
     * 以最省内存的方式读取本地资源的图片  解决OOM异常
     * @param context
     * @param resId
     * @return
     */

    public  Bitmap readBitMap(Context context, int resId){

        BitmapFactory.Options opt = new BitmapFactory.Options();

        opt.inPreferredConfig = Bitmap.Config.RGB_565;

        opt.inPurgeable = true;

        opt.inInputShareable = true;

        //获取资源图片

        InputStream is = context.getResources().openRawResource(resId);

        return BitmapFactory.decodeStream(is,null,opt);

    }


}
